package wavedigest.data;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.wave.api.Event;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class WaveChange
{
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private String modifiedBy;
	
	@Persistent
	private String changeDescription;
	
	@Persistent
	private Date changeTime;
	
	@Persistent
	private RegisteredWave registeredWave;
	
	public WaveChange()
	{
	
	}
	
	public WaveChange(Event evt)
	{
		super();
		
		this.changeTime = new Date(evt.getTimestamp());
		this.modifiedBy = evt.getModifiedBy();
		createChangeDescription(evt);
	}

	private void createChangeDescription(Event evt)
	{
		StringBuilder description = new StringBuilder();
		
		if (null != evt.getChangedTitle())
			description.append("Title Changed: " + evt.getChangedTitle() + "\n");
		
//		if (null != evt.getAddedParticipants() && evt.getAddedParticipants().size() > 0)
//		{
//			description.append("Added Participants: ");
//			boolean bFirst = true;
//			for (String newParticipant : evt.getAddedParticipants())
//			{
//				if (!bFirst)
//					description.append(", ");
//				
//				bFirst = false;
//				description.append(newParticipant);
//			}
//			
//			description.append("\n");
//		}
//		
//		if (null != evt.getRemovedParticipants() && evt.getRemovedParticipants().size() > 0)
//		{
//			description.append("Removed Participants: ");
//			boolean bFirst = true;
//			for (String newParticipant : evt.getRemovedParticipants())
//			{
//				if (!bFirst)
//					description.append(", ");
//				
//				bFirst = false;
//				description.append(newParticipant);
//			}
//			
//			description.append("\n");
//		}
		
		if (null != evt.getCreatedBlipId())
			description.append("Added Blip: " + evt.getCreatedBlipId() + "\n");

		if (null != evt.getRemovedBlipId())
			description.append("Added Blip: " + evt.getRemovedBlipId() + "\n");

		if (null != evt.getBlip())
		{
			description.append("updated blip text for blip id " + evt.getBlip().getBlipId() + ": \n-----\n" + evt.getBlip().getDocument().getText() + "\n-----\n");
		}
		
		this.changeDescription = description.toString();
	}

	public Key getKey()
	{
		return key;
	}

	public void setKey(Key key)
	{
		this.key = key;
	}

	public String getModifiedBy()
	{
		return modifiedBy;
	}

	public void setModifiedBy(String modifiedBy)
	{
		this.modifiedBy = modifiedBy;
	}

	public String getChangeDescription()
	{
		return changeDescription;
	}

	public void setChangeDescription(String changeDescription)
	{
		this.changeDescription = changeDescription;
	}

	public Date getChangeTime()
	{
		return changeTime;
	}

	public void setChangeTime(Date changeTime)
	{
		this.changeTime = changeTime;
	}

	public RegisteredWave getRegisteredWave()
	{
		return registeredWave;
	}

	public void setRegisteredWave(RegisteredWave registeredWave)
	{
		this.registeredWave = registeredWave;
	}
}
